package com.wmlive.hhvideo.widget;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.TextView;

import com.wmlive.hhvideo.utils.KLog;

/**
 * Created by vhawk on 2017/6/19.
 */

public class KeyPreImeEditText extends AppCompatEditText {

    private static final String TAG = "KeyPreImeEditText";

    private KeyBoardDismissListener keyBoardDismissListener;

    public KeyPreImeEditText(Context context) {
        super(context);
    }

    public KeyPreImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public KeyPreImeEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {


                KLog.v(TAG,"event = "+event.getKeyCode());


                return false;
            }
        });
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            Log.e("keyboard", "onKeyPreIme we got back");
            if (keyBoardDismissListener != null){
                keyBoardDismissListener.onKeyDismiss();
            }

        }
        return super.onKeyPreIme(keyCode, event);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {


        KLog.v(TAG,"event = "+event.getKeyCode());



        return super.dispatchKeyEvent(event);
    }

    public KeyPreImeEditText setKeyBoardDismissListener(KeyBoardDismissListener keyBoardDismissListener) {
        this.keyBoardDismissListener = keyBoardDismissListener;
        return this;
    }

    public interface KeyBoardDismissListener{
        void onKeyDismiss();
    }
}
